<template>
  <div>
      <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加角色 -->
    <div style="margin-top: 15px">
      <el-button type="success" plain @click='adddialogFormVisible = true'>添加角色</el-button>
    </div>
    <!-- 角色列表表格 -->
    <template>
      <el-table
        :data="roleList"
        style="width: 100%; margin-top:15px"
        border>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-row :gutter="10" v-for="frist in props.row.children" :key="frist.id" style="margin-bottom: 10px">
              <el-col :span="4">
                <el-tag
                  closable
                  @close='closeRolePower(props.row,frist.id)'
                  type="success">
                  {{frist.authName}}
                </el-tag>
              </el-col>
              <el-col :span="20">
                <el-row :gutter="0" v-for="second in frist.children" :key="second.id" style="margin-bottom: 8px">
                  <el-col :span="4">
                    <el-tag
                      closable
                      @close='closeRolePower(props.row,second.id)'
                      type="warning">
                      {{second.authName}}
                    </el-tag>
                  </el-col>
                  <el-col :span="20">
                    <el-row :gutter="0">
                      <el-col :span="24">
                        <el-tag
                          v-for="third in second.children" :key="third.id"
                          closable
                          @close='closeRolePower(props.row,third.id)'
                          type="info"
                          style="margin-bottom: 8px; margin-right:10px;">
                          {{third.authName}}
                        </el-tag>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column
          label="角色名称"
          prop="roleName">
        </el-table-column>
        <el-table-column
          label="描述"
          prop="roleDesc">
        </el-table-column>
        <el-table-column label="操作" prop="desc">
          <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <el-button type="primary" icon="el-icon-edit" plain></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <el-button type="primary" icon="el-icon-delete" plain></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="分配权限" placement="top">
            <el-button type="primary" icon="el-icon-share" plain @click="openGrantForm(scope.row.id)"></el-button>
          </el-tooltip>
        </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分配权限列表框 -->
    <el-dialog title="分配权限" :visible.sync="grantFormVisible">
      <div style="height:400px;overflow:auto">
        <el-tree
          :data="grantList"
          show-checkbox
          node-key="id"
          ref="tree"
          default-expand-all="true"
          :default-checked-keys="checkList"
          :props="defaultProps">
        </el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="grantFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="grantRoleForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// , grantRoleRight
import {getUserPowerList, deleteRolePower, getPowerList, grantRoleRight} from '@/api/index.js'
export default {
  data () {
    return {
      roleId: '',
      roleList: [],
      grantFormVisible: false,
      grantList: [],
      // 默认勾选数组
      checkList: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      }
    }
  },
  mounted () {
    this.hadPowerList()
  },
  methods: {
    hadPowerList () {
      getUserPowerList().then(res => {
        this.roleList = res.data
      })
    },
    // 删除角色权限
    closeRolePower (row, rightId) {
      deleteRolePower({roleId: row.id, rightId: rightId}).then(res => {
        if (res.meta.status === 200) {
          console.log(res)
          // 添加成功信息
          this.$message.success(res.meta.msg)
          // 重新刷新列表（因为返回的数据有删除后的列表）
          row.children = res.data
        } else {
          this.$message.error(res.meta.msg)
        }
      })
    },
    // 打开授权框
    openGrantForm (id) {
      // 开列表
      this.grantFormVisible = true
      // 获取分配角色权限列表
      getPowerList('tree').then(res => {
        this.grantList = res.data
      })
      this.roleId = id
      console.log(id)
    },
    // 获取授权列表的默认勾选项
    grantRoleForm () {
      // 获取对象数组，需要获取其中的pid和id
      var checkArr = this.$refs.tree.getCheckedNodes()
      // 获取数组元素的平方根
      var nodeArr = checkArr.map(val => {
        // 返回id 和 pid
        return val.id + ',' + val.pid
      })
      // 把分开的数组融合在一起
      var idArr = nodeArr.join(',')
      // 再次把字符串变为数组
      var moreArr = idArr.split(',')
      // 最后把多余的数组值去掉，Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组
      var arr = Array.from(new Set(moreArr))
      // 最后再变成字符串
      var str = arr.join(',')
      // 获取角色授权
      grantRoleRight({roleId: this.roleId, rids: str}).then(res => {
        if (res.meta.status === 200) {
          console.log(res)
          // 添加成功信息
          this.$message.success(res.meta.msg)
        } else {
          this.$message.error(res.meta.msg)
        }
        // 关闭信息框
        this.grantFormVisible = false
        // 更新页面
        this.hadPowerList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
